package org.tostring.pork;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.pork.criteria.AccountCriteria;
import org.pork.criteria.MinutesCriteria;
import org.pork.criteria.TeamCriteria;
import org.pork.criteria.W_Criteria;
import org.pork.domain.AccountReplyVO;
import org.pork.domain.AccountVO;
import org.pork.domain.DepartmentVO;
import org.pork.domain.EmployeesVO;
import org.pork.domain.MessengerVO;
import org.pork.domain.MinutesReplyVO;
import org.pork.domain.MinutesVO;
import org.pork.domain.TeamBbsVO;
import org.pork.domain.TeamReplyVO;
import org.pork.domain.WorkReportVO;
import org.pork.mapper.WorkReportMapper;
import org.pork.service.AccountService;
import org.pork.service.EmployeesService;
import org.pork.service.MinutesService;
import org.pork.service.TeamBbsService;
import org.pork.util.AccountPosition;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/")
public class MainControl {

	private static final Logger logger = Logger.getLogger(MainControl.class);
	
	
	@Inject
	AccountService aService;
	@Inject
	EmployeesService ePService;
	@Inject
	MinutesService minService;
	@Inject
	TeamBbsService tBbsService;
	@Inject
	WorkReportMapper wMapper;
	
	
	@RequestMapping(method = RequestMethod.GET)
	public String main(HttpSession session) {
		
		if(session.getAttribute("userLoginInfo") != null){
			return "main";
		}
		
		return "login";
		
	}

	
	
	
	//JSP 페이지로 화면 이동
	@RequestMapping(value="login", method = RequestMethod.GET)
	public String fileUploadForm(HttpSession session) {
		
		if(session.getAttribute("userLoginInfo") != null){
			return "main";
		}
		
		return "login";
		
	}
	
	
	
	
	

    // 로그아웃
    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.setAttribute("userLoginInfo", null);
        return "login";
    }
    
    
    
    
    
    
    // 로그인 처리
    @RequestMapping(method = RequestMethod.POST)
    public String  loginProcess(EmployeesVO eVO, HttpSession session, HttpServletRequest request) {
    	
    	logger.info("loginProcess................");
        
        logger.info("loginProcess................1");
        
 
        logger.info("loginProcess................2");
        
        EmployeesVO epVO = null;
        
		try {
			epVO = ePService.view(eVO.getEpid());
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		
		}
        
        logger.info("loginProcess................3");
        
        
       if(epVO.getEpid().equals(epVO.getEpid()) && eVO.getPw().equals(eVO.getPw())){
     
        	session.setAttribute("userLoginInfo", epVO);
        	 
        
        }
       
        return "main";
    }
	
	
    
    //부서 그룹 가져오기
    @RequestMapping(value="messegerGroup", method = RequestMethod.GET)
    public @ResponseBody List<DepartmentVO> messegerGroup(){
    	
    	logger.info("messegerGetter..............");
    	

    	
    	List<DepartmentVO> dvlist = new ArrayList<DepartmentVO>();
    	
    	try {
    		dvlist = ePService.departmentList();
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	
    	return dvlist;
    	
    }    
    
    
    
    //메신져 친구 목록 가져오기
    @RequestMapping(value="messegerGetter", method = RequestMethod.GET)
    public @ResponseBody List<MessengerVO> messegerGetter(String departCode){
    	
    	logger.info("messegerGetter..............");
    	
    	
    	logger.info(departCode);
    	
    	List<MessengerVO> messengerVO = new ArrayList<MessengerVO>();
    	
    	try {
    		messengerVO = ePService.messegerList(departCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	
    	return messengerVO;
    	
    }
    
    
    
    //main연결 
    @RequestMapping(value="main", method=RequestMethod.GET)
	public String main(){		
			
		
		return "main";
		
	}

    
    
    
	//거래처 등록 get 방식
	@RequestMapping(value="accountregist", method=RequestMethod.GET)
	public String accountRegist(){		
			
		
		return "accountregist";
		
	}
	
	
	
	
	//거래처 등록 post 방식
	@RequestMapping(value="accountregist", method=RequestMethod.POST)
	public String accountRegistAction(AccountVO vo){
		
		
		logger.info("registAcoount.............." );
		logger.info(vo.toString());
			
		String findkey = "";
		findkey += vo.getAddr1();
		findkey += vo.getBunji();
		findkey = findkey.trim().replace(" ", "");
			
			
		AccountPosition ap = new AccountPosition();
		
		vo.setLatitude(ap.getGpsY(findkey));
		vo.setLongitude(ap.getGpsX(findkey));		
		
		logger.info(vo.toString());
		try {
			aService.regist(vo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "accountlist";						
	}
	
	
	
	
	
	//거래처 리스트 보기
	@RequestMapping(value="accountlist", method=RequestMethod.GET)
	public String accountList(AccountCriteria aCri, Model model){
		
		
		logger.info("AccountList.............." );
		
		return "accountlist";						
	}


	
	
	//거래처 리스트 목록 가져오기
	@RequestMapping(value="accountlistAction", method=RequestMethod.GET)
	public @ResponseBody List<AccountVO> accountListAction(AccountCriteria aCri){
		
		logger.info("accountlistAction.............");
		
		logger.info(aCri.getKeyword());
		
		logger.info(aCri.getKeywordType());
		
		logger.info(aCri.getStateType());
		
		List <AccountVO> list = null;
		
		if(aCri.getKeyword()==""){aCri.setKeyword(null);}
		if(aCri.getStateType()==""){aCri.setStateType(null);}
		try {
			list = aService.list(aCri);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		logger.info(list.toString());
		
		
		return list;
	}
	
	
	
	
	//거래처 상세 뷰로 가기
	@RequestMapping(value="accountview", method=RequestMethod.GET)
	public String accountview(String accountCode, Model model){
		
		logger.info("accountview.............");
		
		logger.info("........" + accountCode);
		
		AccountVO aVO = null;
		
		try {
			aVO = aService.view(accountCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		logger.info(aVO.toString());
		
		model.addAttribute("aVO", aVO);
		
		
		return "accountview";
	}
	
	
	
	//거래처 댓글 리스트
	@RequestMapping(value="accountReplyAction", method=RequestMethod.GET)
	public @ResponseBody List<AccountReplyVO> accountReply(String accountCode, Model model){
		
		logger.info("accountReply.............");
		
		List<AccountReplyVO> aRList = null;
		
		try {
			aRList = aService.replyList(accountCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		logger.info(aRList.toString());
		
		
		return aRList;
		
	}
	
	
	
	//회의록 작성 페이지로
	@RequestMapping(value="minutesregist", method=RequestMethod.GET)
	public String minutesregist(){
		
		logger.info("minuteregist...............");
		
		
		return "minutesregist";
		
	}
	
	
	
	//회의록 DB 저장 작업
	@RequestMapping(value="minutesregistAction", method=RequestMethod.POST)
	public String minutesregistAction(MinutesVO minVO){
		
		logger.info("minutesregistAction...........");
		
		logger.info(minVO.toString());
		
		try {
			minService.regist(minVO);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "main";
		
	}
	
	
	
	//회의록 리스트 페이지
	@RequestMapping(value="minuteslist", method=RequestMethod.GET)
	public String minuteslist(){
		
		logger.info("minuteslist...........");
		
		return "minuteslist";
		
	}
	
	
	//회의록 리스트 받아오기
	@RequestMapping(value="minuteslist", method=RequestMethod.POST)
	public @ResponseBody List<MinutesVO> minuteslistAction(MinutesCriteria minCri){
		
		logger.info("minuteslistAction...........");
		
		List<MinutesVO> list = new ArrayList<MinutesVO>();
		
		try {
			list =minService.list(minCri);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
		
	}
	
	
	
	//회의록 상세 보기
	@RequestMapping(value="minutesview", method=RequestMethod.GET)
	public String minutesview(Integer seqno, Model model){
		
		logger.info("minutesview...........");
		
		MinutesVO minVO = new MinutesVO();
		
		try {
			
			minVO = minService.view(seqno);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		model.addAttribute("minVO", minVO);
		
		logger.info("minutesview...........2");
		
		return "minutesview";
		
	}
	
	
	//회의록 리플 보기
	@RequestMapping(value="minutesviewreply", method=RequestMethod.GET)
	public @ResponseBody List<MinutesReplyVO> minutesviewreply(Integer seqno){
		
		logger.info("minutesviewreply...........");
		
		List<MinutesReplyVO> listMinReVO = new ArrayList<MinutesReplyVO>();
		
		try {
			listMinReVO = minService.listReply(seqno);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		return listMinReVO;
		
	}
	
	
	//회의록 리플 등록
	@RequestMapping(value="minutesregistaction", method=RequestMethod.POST)
	public void minutesviewreply(MinutesReplyVO minReVO){
		
		logger.info("minutesregistaction...........");
		
		try {
			minService.registReply(minReVO);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	/////////////////////////////////////////////////////////////////////////////
	// by 형지
	
	
	//회원가입 
		@RequestMapping(value="join", method=RequestMethod.POST)
		public void join(EmployeesVO vo){
			
			
			logger.info("회원가입.............." );
			logger.info(vo.toString());
				
				
			try {
				ePService.regist(vo);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	/////////////////////////////////팀게시판

		
	
		
		@RequestMapping(value="teambbsregist", method=RequestMethod.GET)
		public String teambbsregist(){		
				
			return "teambbsregist";
			
		}
		
		@RequestMapping(value="teambbsregist", method=RequestMethod.POST)
		public void TeamBbsRegist(TeamBbsVO vo){
		
			logger.info(vo);
			
			try {
				tBbsService.regist(vo);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
						
		}
		
		@RequestMapping(value="teambbslist", method=RequestMethod.GET)
		public String teambbslist(HttpSession session, TeamCriteria cri, Model model){		
			
			logger.info("get 팀리스트" + cri);
			
			// logger.info(session.getAttribute("userLoginInfo").toString());
			
			EmployeesVO epVO = (EmployeesVO) session.getAttribute("userLoginInfo");
			
			TeamCriteria tCri = new TeamCriteria();
			
		//	logger.info("teambbslist..............1" + cri.getPage());
		//	logger.info("teambbslist..............1" + cri.getKeyword());
			
			tCri.setDepartCode(epVO.getDepartCode());
			tCri.setEpid(epVO.getEpid());
			
			tCri.setTotalCount(cri.getTotalCount());
			tCri.setPage(cri.getPage());
			tCri.setKeyword(cri.getKeyword());
			tCri.setKeywordType(cri.getKeywordType());
			
			List<TeamBbsVO> tBbsList=null;			
			List<TeamBbsVO> tNoticeList=null;	

			 logger.info("teambbslist..............2"+ tCri);
			
			try {
				tBbsList = tBbsService.list(tCri);
				tNoticeList = tBbsService.noticeList(tCri);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			logger.info(tBbsList.toString());
			//logger.info("teambbslist..............3");
			
			
			model.addAttribute("tCri", tCri);
			model.addAttribute("tBbsList", tBbsList);
			model.addAttribute("tNoticeList", tNoticeList);
			
			//logger.info("teambbslist..............4");
			 
			return "teambbslist";
	}
		
		
		@RequestMapping(value="teambbslist", method=RequestMethod.POST)
		@ResponseBody
		public List<TeamBbsVO> TeamBbsList(TeamCriteria cri, HttpSession session){
		
			logger.info("포스트 콘트롤 들어오는 크리 : " + cri);
			
			EmployeesVO epVO = (EmployeesVO) session.getAttribute("userLoginInfo");
			
			TeamCriteria tCri = new TeamCriteria();
			
			tCri.setPage(cri.getPage());
			tCri.setKeyword(cri.getKeyword());
			tCri.setKeywordType(cri.getKeywordType());
			
			tCri.setDepartCode(epVO.getDepartCode());
			tCri.setEpid(epVO.getEpid());
			
			logger.info(tCri);
			
			List<TeamBbsVO> tBbsList=null;
			
			try {
				tBbsList = tBbsService.list(tCri);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			logger.info(tBbsList);
			return tBbsList; 
						
		}
	
			
		@RequestMapping(value="teambbsview", method=RequestMethod.GET)
		public String teamListView(TeamBbsVO vo, Model model){		
				
			logger.info("-------------------클제목 클릭");
			logger.info("---------teambbsview----------" + vo);
			
			TeamBbsVO data = null;
			List<TeamReplyVO> replyList = null;
			try {
				data = tBbsService.view(vo);
				replyList = tBbsService.replyList(vo.getSeqno());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			logger.info("-------data------------" + data);
			
			model.addAttribute("vo", data);
			model.addAttribute("replyList", replyList);
			
			return "teambbsview";
			
		}

		// 글수정
		
		@RequestMapping(value="teambbsupdate", method=RequestMethod.POST)
		public String teamBbsUpdate(TeamBbsVO vo){		
				
			logger.info("---------업데이트----------" + vo);
			
			try {
				tBbsService.modify(vo);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return "teambbsview";
			
		}
		
		
		///////////////리플//////////////////////////
		
		@RequestMapping(value="teamreplyregist", method=RequestMethod.POST)
		public String TeamReplyRegist(TeamReplyVO vo){
		
			logger.info("reply regist " + vo);
			
			try {
				tBbsService.replyRegist(vo);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return "teambbsview";
			
		}
	//////////////////////////////////////////////////////////////////////////////////
    // by 효준
		
		@RequestMapping(value="reportingRegist", method=RequestMethod.GET)
		public String reportRegistForm(Model model){		
				
			logger.info("------reportingRegistForm 들어옴------------------");
						
			return "reportingRegist";
			
		}

		
		@RequestMapping(value="reportingRegist", method=RequestMethod.POST)
		public String reportRegistAction(WorkReportVO vo, Model model){		
				
			logger.info("------reportingRegistAction 들어옴------------------");
			logger.info(vo);
			
			String result = "main";

			try {
				wMapper.create(vo);
				
				
				switch (vo.getReportType()) {
				
				case "0":
					
					result = "todayReporting";
					break;
					
				case "1":
					result = "weekReporting";
					break;

				case "2":
					result = "monthReporting";
					break;
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
						
			return result;
			
		}
		
		@RequestMapping(value="todayReporting", method=RequestMethod.GET)
		public String todayReportView(W_Criteria cri, HttpSession session, Model model){
			
			logger.info("------todayReportView-------------------");
			logger.info(cri);
			EmployeesVO vo = (EmployeesVO) session.getAttribute("userLoginInfo");
			
			if(vo.getGradeLev() <= 6)
				cri.setQuery("AND reporttype='0' ");
			else
				cri.setQuery("AND reporttype='0' writer='"+vo.getEpid()+"' ");
			
			List<WorkReportVO> list = wMapper.list(cri);
			
			logger.info(list);
			
			model.addAttribute("list", list);
			
			return "todayReporting";
		}
			
		@RequestMapping(value="weekReporting", method=RequestMethod.GET)
		public String weekReportView(W_Criteria cri, HttpSession session, Model model){
			
			logger.info("------weekReportView-------------------");
			logger.info(cri);
			EmployeesVO vo = (EmployeesVO) session.getAttribute("userLoginInfo");
			
			if(vo.getGradeLev() <= 6)
				cri.setQuery("AND reporttype='1' ");
			else
				cri.setQuery("AND reporttype='1' writer='"+vo.getEpid()+"' ");
			
			List<WorkReportVO> list = wMapper.list(cri);
			
			logger.info(list);
			
			model.addAttribute("list", list);
			
			return "weekReporting";
		}
		@RequestMapping(value="monthReporting", method=RequestMethod.GET)
		public String monthReportView(W_Criteria cri, HttpSession session, Model model){
			
			logger.info("------monthReportView-------------------");
			logger.info(cri);
			EmployeesVO vo = (EmployeesVO) session.getAttribute("userLoginInfo");
			
			if(vo.getGradeLev() <= 6)
				cri.setQuery("AND reporttype='2' ");
			else
				cri.setQuery("AND reporttype='2' writer='"+vo.getEpid()+"' ");
			
			List<WorkReportVO> list = wMapper.list(cri);
			
			logger.info(list);
			
			model.addAttribute("list", list);
			
			return "monthReporting";
		}
	
}























